package mobile.android.Hotel;

import java.util.ArrayList;

import mobile.android.Hotel.Hotel;
import mobile.android.Hotel.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter{
	
	private Context context;

    private ArrayList<String> groups;

    private ArrayList<ArrayList<Hotel>> children;
    
    public ExpandableListAdapter(Context context, ArrayList<String> groups,
            ArrayList<ArrayList<Hotel>> children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
       
    }

    public void adGroup(){
    	groups.add("5 Star");
    	groups.add("4 Star");
    	groups.add("3 Star");
    	groups.add("2 Star");
    	groups.add("1 Star");
    }
    
    public void addItem(Hotel hotel) {
        if (!groups.contains(hotel.getLv())) {
            groups.add(hotel.getLv());
        }
    	int index = groups.indexOf(hotel.getLv());
        if (children.size() < index + 1) {
            children.add(new ArrayList<Hotel>());
        }
        children.get(index).add(hotel);
    }
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return children.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Hotel hotel = (Hotel) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvName);
        tv.setText("   " + hotel.getName());
        TextView tv1 = (TextView) convertView.findViewById(R.id.tvPrince);
        tv1.setText("   " + hotel.getPrince());
        return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return children.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(group);
        return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
